import store from "../index";
/**
 * AppTypes 应用类型
 * AllPageJSON 当前页面中所有数据信息
 * PageIdx 当前页面ID
 * nowType 当前组件类型
 */
const state = {
  AppTypes: "",
  PageIdx: "",
  nowType: "",
  ApplyID: Number,
  componentIdx: Number,
  AllPageJSON: [],
  isSave: false
};

const mutations = {
  SET_AppTypes: (state, AppTypes) => {
    state.AppTypes = AppTypes;
  },
  SET_PageIdx: (state, PageIdx) => {
    state.PageIdx = PageIdx;
  },
  SET_PageJSON: (state, AllPageJSON) => {
    state.AllPageJSON = AllPageJSON;
    localStorage.setItem("page_json", JSON.stringify(AllPageJSON));
  },
  SET_NowType: (state, nowType) => {
    state.nowType = nowType;
  },
  SET_ComIdx: (state, componentIdx) => {
    state.componentIdx = componentIdx;
  },
  SET_ApplyID: (state, ApplyID) => {
    state.ApplyID = ApplyID;
  },
  SET_ISSAVE: (state, isSave) => {
    state.isSave = isSave;
  }
};

const actions = {
  // 更新组件设置数据内容
  PageJSONUpData({ commit, state }) {
    console.log(1212)
    state.AllPageJSON.map(item => {
      console.log(item.id, state)
      if (item.id === Number(state.PageIdx)) {
        // 当前组件类型
        for (const i in JSON.parse(item.page_json)) {
          if (JSON.parse(item.page_json)[i].type === state.nowType) {
            if (
              Number(JSON.parse(item.page_json)[i].id) ===
              Number(state.componentIdx)
            ) {
              // 执行更新
              upDataCon(state.nowType, state.componentIdx, commit, state);
            }
          }
        }
      }
    });
  }
};

// 更新AllPageJSON内容
/**
 * 页面 => 组件 => 类型 => 组件id => 设置内容
 * @param {当前组件idx} arridx
 * @param {更新当前组件类型} type
 * @param {} comidx
 * @param {*} param3
 */
function upDataCon(type, comidx, commit, state) {
  // return new Promise()
  // var arrData = [];
  const arr = state.AllPageJSON;
  for (let i = 0; i < arr.length; i++) {
    // 当前页
    if (arr[i].id === Number(state.PageIdx)) {
      // 当前组件类型
      var arrItem = JSON.parse(arr[i].page_json);
      console.log(type)
      switch (type) {
        case "DiyRubiks":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj = store.state.Rubiks.rubils;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyMarket":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj = store.state.merket.merketObj;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyShopmarket":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj = store.state.shopMarket.shopMarket;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiySearch":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj = store.state.search.searchObj;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyActivity":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj = store.state.activity.activityList;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyNav":
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              arrItem[y].data = store.state.nav.ObjCon;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyAdvert":
          console.log(arrItem)
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              arrItem[y].data = store.state.advert.advertObj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyText":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj2 = {
                wordStyle: store.state.text.wordStyle,
                idx: store.state.text.idx
              };
              arrItem[y].data = obj2;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyBD":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj2 = {
                bdobj: store.state.notice.bdobj
              };
              arrItem[y].data = obj2;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyBtn":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj2 = {
                btnStyle: store.state.btn.btnStyle,
                borderStyle: store.state.btn.borderStyle,
                word: store.state.btn.word
              };
              arrItem[y].data = obj2;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyTabar":
          // 当前组件
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              // 重新赋值
              const obj1 = {
                WordColor: store.state.tarbar.WordColor,
                checkColor: store.state.tarbar.checkColor,
                barColor: store.state.tarbar.barColor,
                navstyle: store.state.tarbar.navstyle,
                radio: store.state.tarbar.radio,
                idx: store.state.tarbar.idx,
                tarbar: store.state.tarbar.tarbar
              };
              arrItem[y].data = obj1;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyImg":
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              const obj = {
                width: store.state.imgs.ImgObj.width,
                height: store.state.imgs.ImgObj.height,
                borderRadius: store.state.imgs.ImgObj.borderRadius,
                url: store.state.imgs.ImgObj.url
              };
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiySwiper":
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              const obj = store.state.myswiper.swiperObj;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyVideo":
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              const obj = {
                setvideo: store.state.video.setvideo
              };
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyList":
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              const obj = store.state.list.listPageStyle;
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        case "DiyFree":
          for (const y in arrItem) {
            if (arrItem[y].id === comidx) {
              const obj = {
                freeStyle: store.state.free.freeStyle
              };
              arrItem[y].data = obj;
              arr[i].page_json = JSON.stringify(arrItem);
            }
          }
          break;
        default:
          break;
      }
    }
  }
  commit("SET_PageJSON", arr);
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
